import request from '../../utils/request.js'
import PubSub from 'pubsub-js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day:0,
    month:0,
    recommendList:[],
    index:''

  },

//跳转到音乐详情
  toSongDetail(event){
 let song = event.currentTarget.dataset.song;
 let index = event.currentTarget.id;


wx.navigateTo({

  url: '/pages/songDetail/songDetail?musicId='+song.id+"&index="+index

})

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    //查看用户是否登录
   let userInfo = wx.getStorageSync("userInfo");
   if(!userInfo){
     wx:wx.showToast({
       title: '请先登录',
       icon: 'none',
       success: function(res) {
         //跳转到登录页面
         wx.reLaunch({
           url: '/pages/login/login'
         
         });
       },
       
     })
   }
    this.setData({
      day:new Date().getDate(),
      month:new Date().getMonth()+1
    })

    //获取每日推荐数据
    this.getRecommendList();

    //订阅消息
    PubSub.subscribe("switchType",(msg,type)=>{
      let { recommendList,index}=this.data;

      if(type === 'shang'){
        (index ==0) && (index=recommendList.length);
        index--;
      }else{
        (index==recommendList.length-1) && (index=-1);
        index++;
      }

      //更新下标
      this.setData({
        index
      })

      let musicId = recommendList[index].id;
      PubSub.publish("musicId",musicId);

    })

  },

  async getRecommendList(){
   let recommendList = await request('/recommend/songs');

   this.setData({
     recommendList:recommendList.recommend
   })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})